<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @author jay
 */
class Articles extends CI_Controller{
 	
	function index() { 
		
	}
	
 	function createNew() { 
 		$this->load->view('articles_view');
 	}

 	function save() {
 		$articlecontents = $this->input->post('articlecontents');
		$category = $this->input->post('category');
		$userid = $this->input->post('userid');
		$articleid = $this->input->post('articleid');
 		

 		$data = array(
 			"articlecontents" 	=> $articlecontents,
 			"category" 			=> $category,
 			"userid" 			=> $userid,
			"articleid"			=> $articleid
 		);
 		

 		// save data to database
 		$this->load->model('articles_model');
 		$this->articles_model->save($data);

 		redirect('Home');
 	}

 	function getArticles() {
 		$category = $this->input->post('category');

 		$this->load->model('articles_model');
 		$data['articles'] = $this->articles_model->getArticles($category);
 		echo json_encode($data);
 	}

 	function view() {
 		$id = $this->input->get('id');
 		$this->load->model('articles_model');
 		$data['article'] = $this->articles_model->getArticleForID($id);

 		$this->load->view('viewarticle_view', $data);
 	}

	function editArticle() {
		$id = $this->input->get('id');
 		$this->load->model('articles_model');
 		$data['article'] = $this->articles_model->getArticleForID($id);
		
		$this->load->view('editarticle_view', $data);
	}
	
	function updateLike() {
		$userid = $this->input->post('userid');
		$articleid = $this->input->post('articleid');
		
		//echo 'userid = ' . $userid;
		//echo ' articleid = ' . $articleid;
		
		$this->load->model('articles_model');
		$data['likes'] = $this->articles_model->updateLike($userid,$articleid);
		
		header('Content-Type: application/json; charset=utf-8');
		echo json_encode($data);
	}
	
}
?>